package com.muyu.danmo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoTrainingInformation;
import com.muyu.danmo.domain.DanmoTrainingShoppingProducts;
import com.muyu.danmo.service.IDanmoTrainingShoppingProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 培训班商品Controller
 * 
 * @author kunzong
 * @date 2024-11-28
 */
@RestController
@RequestMapping("/DanmoTrainingShoppingProducts")
public class DanmoTrainingShoppingProductsController extends BaseController
{
    @Autowired
    private IDanmoTrainingShoppingProductsService danmoTrainingShoppingProductsService;

    /**
     * 查询培训班商品列表
     */
//    @RequiresPermissions("system:DanmoTrainingShoppingProducts:list")
    @GetMapping("/list")
    public Result list(DanmoTrainingShoppingProducts danmoTrainingShoppingProducts)
    {
        PageHelper.startPage(danmoTrainingShoppingProducts.getPageNum(), danmoTrainingShoppingProducts.getPageSize());
        List<DanmoTrainingShoppingProducts> list = danmoTrainingShoppingProductsService.selectDanmoTrainingShoppingProductsList(danmoTrainingShoppingProducts);
        PageInfo<DanmoTrainingShoppingProducts> danmoInformationPageInfo = new PageInfo<>(list);
        return Result.success(danmoInformationPageInfo);
    }

    /**
     * 导出培训班商品列表
     */
//    @RequiresPermissions("system:DanmoTrainingShoppingProducts:export")
    @Log(title = "培训班商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoTrainingShoppingProducts danmoTrainingShoppingProducts)
    {
        List<DanmoTrainingShoppingProducts> list = danmoTrainingShoppingProductsService.selectDanmoTrainingShoppingProductsList(danmoTrainingShoppingProducts);
        ExcelUtil<DanmoTrainingShoppingProducts> util = new ExcelUtil<DanmoTrainingShoppingProducts>(DanmoTrainingShoppingProducts.class);
        util.exportExcel(response, list, "培训班商品数据");
    }

    /**
     * 获取培训班商品详细信息
     */
//    @RequiresPermissions("system:DanmoTrainingShoppingProducts:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return Result.success(danmoTrainingShoppingProductsService.selectDanmoTrainingShoppingProductsById(id));
    }

    /**
     * 新增培训班商品
     */
//    @RequiresPermissions("system:DanmoTrainingShoppingProducts:add")
    @Log(title = "培训班商品", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoTrainingShoppingProducts danmoTrainingShoppingProducts)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return error("请登录");
        }
        danmoTrainingShoppingProducts.setCreateBy(loginUser.getUsername());
        return Result.success(danmoTrainingShoppingProductsService.insertDanmoTrainingShoppingProducts(danmoTrainingShoppingProducts),"添加成功");
    }

    /**
     * 修改培训班商品
     */
//    @RequiresPermissions("system:DanmoTrainingShoppingProducts:edit")
    @Log(title = "培训班商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoTrainingShoppingProducts danmoTrainingShoppingProducts)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return error("请登录");
        }
        int i = danmoTrainingShoppingProductsService.updateDanmoTrainingShoppingProducts(danmoTrainingShoppingProducts);
        if (i == 1){
            return Result.success(i,"修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    /**
     * 删除培训班商品
     */
//    @RequiresPermissions("system:DanmoTrainingShoppingProducts:remove")
    @Log(title = "培训班商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return error("请登录");
        }
        int i = danmoTrainingShoppingProductsService.deleteDanmoTrainingShoppingProductsByIds(ids);
        if (i == 1){
            return Result.success(i,"删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
}
